<?php

namespace App\Admin\Controllers\Seckill;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Admin\Repositories\CmcStoreSeckill;
use App\Admin\Repositories\CmcStoreSeckillRule;
use App\Models\Goods\CmcGood;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CmcStoreSeckillRuleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmcStoreSeckillRule(), function (Grid $grid) {
            Admin::js('js/main/machine_distribution.js');
            $grid->column('id')->sortable();
            $grid->column('seckill_name');
            $grid->column('images')->image($server=null,100,100);
            $grid->column('is_show');
            $grid->column('status')->switch()->filter(Grid\Column\Filter\In::make([
                0=>'未开启',
                1=>'开启'
            ]));
            $grid->column('add_time');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('created_at', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            // 模糊搜索
            $grid->quickSearch('seckill_name');
            // 回收站
            $grid->filter(function ($filter) {
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(\App\Models\Seckill\CmcStoreSeckillRule::class));
                }
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(\App\Models\Seckill\CmcStoreSeckillRule::class));
                }
            });
            // 异步渲染功能
            $grid->async();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcStoreSeckillRule(), function (Show $show) {
            $show->field('id');
            $show->field('seckill_name');
            $show->field('images');
            $show->field('status');
            $show->field('is_show');
            $show->field('add_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmcStoreSeckillRule(), function (Form $form) {
            $form->display('id');
            $form->text('seckill_name')->rules('required')->maxLength(50,'最多添50个字');
            $form->image('images')->rules('required');
            $form->text('status')->rules('required');
            $form->text('is_show')->rules('required');
//            $form->time('add_time', '选择秒杀时间')->append('-')->rules('required');
            $form->embeds('add_time',function ($form){
               $form->timeRange('s','e','秒杀时间段');
            })->saving(function ($v){
                return json_encode($v);
            });
            $form->display('created_at');
            $form->display('updated_at');
            $form->confirm('您确定要提交表单吗？', 'content');
        });
    }
}
